package com.hbmcc.rxjava.chapter01.adapter.base

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.LayoutHelper
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import com.hbmcc.rxjava.chapter01.holder.onProxyCreateViewHolder

/**
 * 创建日期：2020/11/11
 * 描述:用于所有Adapter的基类
 * 作者:支豪亮
 */


@Suppress("UNCHECKED_CAST")
abstract class SubAdapter<VH : RecyclerView.ViewHolder, T>(
    open val layoutId: Int,
    open val list: MutableList<T>?= null,
    open val layoutHelper: LayoutHelper? =null,
    open val count: Int = 0
) : DelegateAdapter.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return onProxyCreateViewHolder(getBindView(parent),viewType)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        onBindProxyViewHolder(holder as VH, position)
    }

    abstract fun onBindProxyViewHolder(holder: VH, position: Int)

    override fun getItemCount(): Int {
        if(count == 0)  return list?.size?:0
        return count
    }

    override fun onCreateLayoutHelper(): LayoutHelper {
        return layoutHelper?: LinearLayoutHelper()
    }

    override fun getItemViewType(position: Int): Int {
        return layoutId
    }

    fun getBindView(parent: ViewGroup): View {
        return LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
    }


}